﻿using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.EntityFrameworkCore;
using MyAppAPI.Context;
using MyAppAPI.Models;
using MyAppAPI.Repositories.interfaceRepo;
using MySql.Data.MySqlClient;
using System.Collections.Generic;

namespace MyAppAPI.Repositories
{
    public class UserMenuRepository : IUserMenuRepository
    {
        public UserContext userContext;
        public UserMenuRepository(UserContext userContext)
        {
            this.userContext = userContext;
        }
        public List<MenuInfo> GetMenuInfo(int userId)
        {
            List<MenuInfo> menuInfos= new List<MenuInfo>();
            var mlist = (from u in userContext.UserMenu
                        join m in userContext.Menu on u.MenuId equals m.MenuId
                        into umlist
                        from um in umlist.DefaultIfEmpty() 
                        orderby um.MenuId
                        where u.UserId == userId
                        select um).ToList();
            foreach (var item in mlist)
            {
                MenuInfo menuinfo = new MenuInfo();
                menuinfo.icon = item.Icon;
                menuinfo.path = item.Path;
                menuinfo.name = item.Name;
                menuinfo.label = item.Label;
                menuinfo.url = item.Url;
                menuInfos.Add(menuinfo);
            }
            return menuInfos;
        }
    }
}
